package org.flown.shop.service;

import jakarta.validation.Valid;
import org.flown.shop.dao.BaseDAO;

import java.lang.reflect.InvocationTargetException;


public class BaseService<T>
{
    protected final BaseDAO<T> dao;
    public BaseService(BaseDAO<T> dao) {this.dao = dao;}



    //SELECT * FROM ${table_name} where id=#{id} and name=#{name}...
    public T select(T entity)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
    {
        return this.selectByID((Integer) entity.getClass().getMethod("getId").invoke(entity));
    }

    //SELECT * FROM ${table_name} where id=#{id}...
    public T selectByID(int id) {return this.dao.selectByID(id);}
    //INSERT INTO ${table_name} (${column_name1}, ${column_name2}) values(#{},#{})
    public int insert(@Valid T entity) {return this.dao.insert(entity);}
    public boolean updateByID(T entity) {return this.dao.updateByID(entity);}
    public int delete(T entity)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
    {
        return this.deleteByID((Integer) entity.getClass().getMethod("getId").invoke(entity));
    }
    public int deleteByID(int id) {return this.dao.deleteByID(id);}
}


